﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;

namespace Common
{
    public class SetMes
    {
        /// <summary>
        /// 设置页面立即过期
        /// </summary>
        public static void overdue()
        {
            System.Web.HttpContext.Current.Response.AddHeader("Pragma", "No-Cache");
            System.Web.HttpContext.Current.Response.Buffer = true;
            System.Web.HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
            System.Web.HttpContext.Current.Response.Expires = 0;
            System.Web.HttpContext.Current.Response.CacheControl = "no-cache";
        }
        /// <summary>
        /// 删除指定会话
        /// </summary>
        /// <param name="strKey"></param>
        public static void RemoveSession(string strKey)
        {
            HttpContext.Current.Session.Remove(strKey);
        }
        /// <summary>
        /// 删除所有会话
        /// </summary>
        /// <param name="strKey"></param>
        public static void RemoveAllSession()
        {
            HttpContext.Current.Session.RemoveAll();
        }
        /// <summary>
        /// 新建会话
        /// </summary>
        /// <param name="strKey"></param>
        /// <param name="strValue"></param>
        public static void SetSession(string strKey, string strValue)
        {
            HttpContext.Current.Session[strKey] = strValue;
        }
        /// <summary>
        /// 替换标签，把&nbsp;&lt;&gt;&quot; 替换为HTML标记
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string returnHtml(string str)
        {
            if (str.Trim() != "")
            {
                str = str.Replace(@"&lt;", "<");
                str = str.Replace(@"&gt;", "<");
                str = str.Replace(@"&nbsp;", " ");
                str = str.Replace(@"&quot;", @"""");
            }
            return str;
        }
        /// <summary>
        /// 生成cookies/让cookies过期 若不使用cookie域 strDomain为空
        /// </summary>
        /// <param name="cookieName">cookies的名称</param>
        /// <param name="time">过期时间（小时为单位）</param>
        /// <param name="strValue">cookies的值（将被HtmlEncode 编码）</param>
        /// <param name="strDomain">cookie域 为空则为默认cookie</param>
        public static void CreatCookies(string cookieName, int time, string strValue,string strDomain)
        {
            HttpCookie aCookie;
            aCookie = new HttpCookie(cookieName);
            aCookie.Value = HttpContext.Current.Server.HtmlEncode(strValue);
            aCookie.Expires = DateTime.Now.AddHours(time);
            if (!string.IsNullOrEmpty(strDomain))
            {
                aCookie.Domain = strDomain;
            }
            HttpContext.Current.Response.Cookies.Add(aCookie);
        }
        ///   <summary>
        ///   过滤HTML标记
        ///   </summary>
        ///   <param   name="Htmlstring">包括HTML的源码   </param>
        ///   <returns>已经去除后的文字</returns>
        public static string NoHTML(string Htmlstring)
        {
            //删除脚本
            Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "",
              RegexOptions.IgnoreCase);
            //删除HTML
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "   ",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "",
              RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "",
              RegexOptions.IgnoreCase);
            Htmlstring.Replace("<", "");
            Htmlstring.Replace(">", "");
            Htmlstring.Replace("\r\n", "");
            return Htmlstring;
        }
    }
}
